package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFNumberWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFNumberWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFNumberWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFNumberWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFNumberWidget widget) {
        ObjectNode ui = node.getUi();

        putString(ui, "prefix", widget.prefix());
        putString(ui, "unit", widget.unit());
        putInt(ui, "precision", widget.precision());
        putInt(ui, "widgetWidth", widget.widgetWidth());
        ui.put("hideStep", widget.hideStep());

    }


    @Override
    public Class[] supports() {
        return new Class[]{SFNumberWidget.class};
    }
}
